<template>
  <div class="ai-chat-bot">
    <!-- 悬浮AI按钮 -->
    <div 
      class="ai-chat-button"
      @click="toggleChat"
      :class="{ 'active': isOpen }"
    >
      <svg v-if="!isOpen" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M20 2H4C2.9 2 2 2.9 2 4V22L6 18H20C21.1 18 22 17.1 22 16V4C22 2.9 21.1 2 20 2ZM20 16H6L4 18V4H20V16Z" fill="currentColor"/>
        <path d="M7 9H17V11H7V9ZM7 12H13V14H7V12Z" fill="currentColor"/>
      </svg>
      <svg v-else width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M19 6.41L17.59 5L12 10.59L6.41 5L5 6.41L10.59 12L5 17.59L6.41 19L12 13.41L17.59 19L19 17.59L13.41 12L19 6.41Z" fill="currentColor"/>
      </svg>
    </div>

    <!-- 对话窗口 -->
    <div class="ai-chat-window" :class="{ 'open': isOpen }">
      <div class="chat-header">
        <div class="chat-title">
          <div class="ai-avatar">
            <div class="avatar-icon assistant"></div>
          </div>
          <span>小Z助手</span>
        </div>
        <button class="close-btn" @click="toggleChat">×</button>
      </div>

      <div class="chat-messages" ref="messagesContainer">
        <div 
          v-for="message in messages" 
          :key="message.id"
          class="message"
          :class="message.role"
        >
          <div class="message-avatar">
            <div class="avatar-icon" :class="message.role"></div>
          </div>
          <div class="message-content">
            <div class="message-text">
              {{ message.content }}
            </div>
            <div class="message-time">{{ message.time }}</div>
          </div>
        </div>
        
        <!-- 加载状态 -->
        <div v-if="isLoading" class="message assistant">
          <div class="message-avatar">
            <div class="avatar-icon assistant"></div>
          </div>
          <div class="message-content">
            <div class="loading-dots">
              <span></span>
              <span></span>
              <span></span>
            </div>
          </div>
        </div>
      </div>

      <div class="chat-input">
        <input
          v-model="inputMessage"
          @keyup.enter="sendMessage"
          placeholder="请输入您的问题..."
          :disabled="isLoading"
        />
        <button 
          @click="sendMessage"
          :disabled="!inputMessage.trim() || isLoading"
          class="send-btn"
        >
          发送
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { sendMessage } from '@/api/ai-chat'

export default {
  name: 'AIChatBot',
  data() {
    return {
      isOpen: false,
      messages: [],
      inputMessage: '',
      isLoading: false
    }
  },
  methods: {
    toggleChat() {
      this.isOpen = !this.isOpen
      if (this.isOpen && this.messages.length === 0) {
        this.addWelcomeMessage()
      }
    },
    
    addWelcomeMessage() {
      this.messages.push({
        id: Date.now(),
        role: 'assistant',
        content: '您好！我是小Z助手，很高兴为您服务。请问有什么可以帮助您的吗？',
        time: this.getCurrentTime()
      })
    },
    
    async sendMessage() {
      if (!this.inputMessage.trim() || this.isLoading) return
      
      const userMessage = this.inputMessage.trim()
      this.inputMessage = ''
      
      // 添加用户消息
      this.messages.push({
        id: Date.now(),
        role: 'user',
        content: userMessage,
        time: this.getCurrentTime()
      })
      
      this.isLoading = true
      this.scrollToBottom()
      
      try {
        const response = await sendMessage(userMessage)
        const aiResponse = response.data.choices[0].message.content
        
        // 直接添加完整的AI回复，不使用打字机效果
        this.messages.push({
          id: Date.now(),
          role: 'assistant',
          content: aiResponse,
          time: this.getCurrentTime()
        })
        
        this.scrollToBottom()
      } catch (error) {
        console.error('AI聊天错误:', error)
        // 简单错误处理
        this.messages.push({
          id: Date.now(),
          role: 'assistant',
          content: '抱歉，我现在无法回答您的问题，请稍后再试。',
          time: this.getCurrentTime()
        })
        
        this.scrollToBottom()
      } finally {
        this.isLoading = false
      }
    },
    
    getCurrentTime() {
      const now = new Date()
      return now.toLocaleTimeString('zh-CN', { 
        hour: '2-digit', 
        minute: '2-digit' 
      })
    },
    
    scrollToBottom() {
      this.$nextTick(() => {
        const container = this.$refs.messagesContainer
        if (container) {
          container.scrollTop = container.scrollHeight
        }
      })
    },
    

  }
}
</script>

<style scoped>
.ai-chat-bot {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 1000;
}

.ai-chat-button {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  z-index: 1001;
}

.ai-chat-button:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 25px rgba(0, 0, 0, 0.2);
}

.ai-chat-button.active {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.ai-chat-window {
  position: absolute;
  bottom: 80px;
  right: 0;
  width: 450px;
  height: 500px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: scale(0) translateY(20px);
  transform-origin: bottom right;
  opacity: 0;
  transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  pointer-events: none;
}

.ai-chat-window.open {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: auto;
}

.chat-header {
  background: #f8f9fa;
  color: #333;
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e9ecef;
}



.chat-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
}

.ai-avatar {
  display: flex;
  align-items: center;
}

.avatar-icon {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
  color: white;
}

.avatar-icon.user {
  background: #667eea;
}

.avatar-icon.assistant {
  background: #28a745;
}

.avatar-icon.user::before {
  content: 'U';
}

.avatar-icon.assistant {
  background: url('@/assets/images/小 Z 助手头像.png') no-repeat center center;
  background-size: cover;
}

.avatar-icon.assistant::before {
  content: '';
}

.close-btn {
  background: none;
  border: none;
  color: #666;
  font-size: 20px;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.3s;
}

.close-btn:hover {
  background: #e9ecef;
  color: #333;
}

.chat-messages {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.message {
  display: flex;
  gap: 10px;
  max-width: 85%;
}

.message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message.assistant {
  align-self: flex-start;
}

.message-avatar {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.message-content {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.message-text {
  padding: 10px 15px;
  border-radius: 18px;
  word-wrap: break-word;
  line-height: 1.4;
  white-space: pre-line;
}

/* 数字列表样式优化 */
.message-text ol {
  margin: 8px 0;
  padding-left: 20px;
}

.message-text ol li {
  margin-bottom: 8px;
  line-height: 1.5;
}

.message-text ul {
  margin: 8px 0;
  padding-left: 20px;
}

.message-text ul li {
  margin-bottom: 6px;
  line-height: 1.5;
}

/* 加粗文本样式 */
.message-text strong {
  font-weight: 600;
  color: #333;
}

.message.user .message-text {
  background: #667eea;
  color: white;
  border-bottom-right-radius: 5px;
}

.message.assistant .message-text {
  background: #f5f5f5;
  color: #333;
  border-bottom-left-radius: 5px;
}

.message-time {
  font-size: 12px;
  color: #999;
  text-align: center;
}

.message.user .message-time {
  text-align: right;
}

.message.assistant .message-time {
  text-align: left;
}

.loading-dots {
  display: flex;
  gap: 4px;
  padding: 10px 15px;
}

.loading-dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ccc;
  animation: loading 1.4s infinite ease-in-out;
}

.loading-dots span:nth-child(1) { animation-delay: -0.32s; }
.loading-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes loading {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

.chat-input {
  padding: 15px 20px;
  border-top: 1px solid #eee;
  display: flex;
  gap: 10px;
}

.chat-input input {
  flex: 1;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 20px;
  outline: none;
  font-size: 14px;
}

.chat-input input:focus {
  border-color: #667eea;
}

.send-btn {
  padding: 10px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.send-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.send-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}





/* 响应式设计 */
@media (max-width: 768px) {
  .ai-chat-window {
    width: 320px;
    height: 400px;
    bottom: 70px;
  }
  
  .ai-chat-button {
    width: 50px;
    height: 50px;
  }
}

@media (max-width: 480px) {
  .ai-chat-window {
    width: 280px;
    height: 350px;
    bottom: 60px;
  }
}
</style> 